package edu.hawaii.myisern.action;

import java.util.ArrayList;
import java.util.List;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationError;
import edu.hawaii.myisern.model.User;

/**
 * Provides a web interface for the MyIsern application.
 * 
 * @author Ed Kim
 * @author Jon Lao
 * @author JianFei Liao
 * @author Jared Sunouchi
 */
public class LoginActionBean extends MyIsernActionBean {

  @Validate(required = true)
  private String username;

  @Validate(required = true)
  private String password;

  private List<User> users = new ArrayList<User>();

  /**
   * Populates user password list.
   */
  public LoginActionBean() {
    users.add(new User("username", "password"));
  }

  /**
   * The username of the user trying to log in.
   * 
   * @param username user.
   * 
   */
  public void setUsername(String username) {
    this.username = username;
  }

  /**
   * The username of the user trying to log in.
   * 
   * @return username user
   */
  public String getUsername() {
    return username;
  }

  /**
   * The password of the user trying to log in.
   * 
   * @param password pw
   */
  public void setPassword(String password) {
    this.password = password;
  }

  /**
   * The password of the user trying to log in.
   * 
   * @return password pw
   */
  public String getPassword() {
    return password;
  }

  /**
   * Login action, validates user.
   * 
   * @return resolution of login error or home page.
   */
  public Resolution login() {
  
    boolean usernameFound = false;
    User visitor = new User(getUsername(), getPassword());
    User member = new User();
    
    for (User user : users) {
      if (user.getUsername().contentEquals(visitor.getUsername())) {
        usernameFound = true;
        member = user;
      }
    }
    if (usernameFound) {
      if (member.getPassword().contentEquals(visitor.getPassword())) { 
        getContext().getRequest().getSession().setAttribute("user", visitor.getUsername());
        return new RedirectResolution("/home.jsp");          
      }
      else {
        ValidationError error = new SimpleError("Password Incorrect");
        getContext().getValidationErrors().add("password", error);
        return getContext().getSourcePageResolution();                     
      }
    }  
    else {
      ValidationError error = new SimpleError("Username Not Found");
      getContext().getValidationErrors().add("username", error);
      return getContext().getSourcePageResolution();
    }
    
    /*
    if (usernameFound == false) {
      ValidationError error = new SimpleError("Username Not Found");
      getContext().getValidationErrors().add("username", error);
      return getContext().getSourcePageResolution();      
    }  
    else {
      if (member.getPassword().contentEquals(visitor.getPassword())) { 
        getContext().getRequest().getSession().setAttribute("user", visitor.getUsername());
        return new RedirectResolution("/home.jsp");          
      }
      else {
        ValidationError error = new SimpleError("Password Incorrect");
        getContext().getValidationErrors().add("password", error);
        return getContext().getSourcePageResolution();                     
      }
    }
    */
  }
}
